// (C)2013 Reesoft
// GifAnimationTestDlg.cpp : implementation file

#include "stdafx.h"
#include "GifAnimationTest.h"
#include "GifAnimationTestDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

struct SPEED_INFO_STRUCT
{
	double Speed;
	PCTSTR DisplayName;
};

SPEED_INFO_STRUCT SpeedInfo[] =
{
	{ -16, _T("-16") },
	{ -8, _T("-8") },
	{ -4, _T("-4") },
	{ -3, _T("-3") },
	{ -2, _T("-2") },
	{ -1.5, _T("-3/2") },
	{ -1, _T("-1") },
	{ -0.75, _T("-3/4") },
	{ -0.5, _T("-1/2") },
	{ -0.25, _T("-1/4") },
	{ -0.1, _T("-1/10") },
	{ 0, _T("0") },
	{ 0.1, _T("1/10") },
	{ 0.25, _T("1/4") },
	{ 0.5, _T("1/2") },
	{ 0.75, _T("3/4") },
	{ 1, _T("1") },
	{ 1.5, _T("3/2") },
	{ 2, _T("2") },
	{ 4, _T("4") },
	{ 8, _T("8") },
	{ 16, _T("16") },
};

// CGifAnimationTestDlg dialog

CGifAnimationTestDlg::CGifAnimationTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGifAnimationTestDlg::IDD, pParent)
	, m_fSpeed(1)
	, m_iMaxFrameRate(100)
	, m_sFrameInfo(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGifAnimationTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Check(pDX, IDC_CHECK_STRETCH, m_bStretch);
	DDX_Control(pDX, IDC_GIF_ANIMATION_CTRL, m_GifAnimationCtrl);
	DDX_Text(pDX, IDC_EDIT_SPEED, m_sDisplaySpeed);
	DDX_Text(pDX, IDC_EDIT_MAX_FRAME_RATE, m_iMaxFrameRate);
	DDX_Control(pDX, IDC_SLIDER_SPEED, m_SpeedSlider);
	DDX_Control(pDX, IDC_SLIDER_MAX_FRAME_RATE, m_MaxFrameRateSlider);
	DDX_Text(pDX, IDC_STATIC_FRAME_INFO, m_sFrameInfo);
}

BEGIN_MESSAGE_MAP(CGifAnimationTestDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_BUTTON_OPEN_IMAGE_FILE, &CGifAnimationTestDlg::OnBnClickedOpenImageFile)
	ON_BN_CLICKED(IDC_BUTTON_LOAD_FROM_BUFFER, &CGifAnimationTestDlg::OnBnClickedLoadFromBuffer)
	ON_BN_CLICKED(IDC_BUTTON_LOAD_FROM_RESOURCE, &CGifAnimationTestDlg::OnBnClickedLoadFromResource)
	ON_BN_CLICKED(IDC_BUTTON_START_ANIMATION, &CGifAnimationTestDlg::OnBnClickedStartAnimation)
	ON_BN_CLICKED(IDC_BUTTON_STOP_ANIMATION, &CGifAnimationTestDlg::OnBnClickedStopAnimation)
	ON_BN_CLICKED(IDC_CHECK_STRETCH, &CGifAnimationTestDlg::OnBnClickedStretch)
	ON_EN_CHANGE(IDC_EDIT_SPEED, &CGifAnimationTestDlg::OnEnChangeSpeed)
	ON_EN_CHANGE(IDC_EDIT_MAX_FRAME_RATE, &CGifAnimationTestDlg::OnEnChangeMaxFrameRate)
END_MESSAGE_MAP()

BOOL CGifAnimationTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	m_SpeedSlider.SetRange(0, _countof(SpeedInfo) - 1);
	m_SpeedSlider.SetPos(_countof(SpeedInfo) * 3 / 4);
	m_sDisplaySpeed = SpeedInfo[_countof(SpeedInfo) * 3 / 4].DisplayName;

	m_MaxFrameRateSlider.SetRange(1, 100);
	m_MaxFrameRateSlider.SetPos(m_iMaxFrameRate);

	UpdateData(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CGifAnimationTestDlg::OnOK()
{
}

void CGifAnimationTestDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CGifAnimationTestDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CGifAnimationTestDlg::OnBnClickedOpenImageFile()
{
	CFileDialog fdlg(TRUE);

	if (fdlg.DoModal() != IDOK)
	{
		return;
	}

	if (m_GifAnimationCtrl.LoadFromFile(fdlg.GetPathName()) != 0)
	{
		AfxMessageBox(_T("Failed to load image from file."), MB_ICONEXCLAMATION);
	}
}

void CGifAnimationTestDlg::OnBnClickedLoadFromBuffer()
{
	CFileDialog fdlg(TRUE);

	if (fdlg.DoModal() != IDOK)
	{
		return;
	}

	CFile File;
	CFileException FileException;

	if (!File.Open(fdlg.GetPathName(), CFile::modeRead, &FileException))
	{
		AfxMessageBox(_T("Failed to open file to read."), MB_ICONEXCLAMATION);
		return;
	}

	ULONG iSize = (ULONG) File.GetLength();

	if (iSize == 0)
	{
		AfxMessageBox(_T("File is empty."), MB_ICONEXCLAMATION);
		return;
	}

	BYTE* pBuffer = new BYTE[iSize];

	if (File.Read(pBuffer, iSize) != iSize)
	{
		AfxMessageBox(_T("Failed to read file."), MB_ICONEXCLAMATION);
		delete[] pBuffer;
		return;
	}

	if (m_GifAnimationCtrl.LoadFromBuffer(pBuffer, iSize) != 0)
	{
		AfxMessageBox(_T("Failed to load image from buffer."), MB_ICONEXCLAMATION);
	}

	delete[] pBuffer;
}

void CGifAnimationTestDlg::OnBnClickedLoadFromResource()
{
	if (m_GifAnimationCtrl.LoadFromResource((LONG) AfxGetInstanceHandle(), IDR_GIF, _T("GIF")) != 0)
	{
		AfxMessageBox(_T("Failed to load image from resource."), MB_ICONEXCLAMATION);
	}
}

void CGifAnimationTestDlg::OnBnClickedStartAnimation()
{
	m_GifAnimationCtrl.StartAnimation();
}

void CGifAnimationTestDlg::OnBnClickedStopAnimation()
{
	m_GifAnimationCtrl.StopAnimation();
}

void CGifAnimationTestDlg::OnBnClickedStretch()
{
	UpdateData();

	m_GifAnimationCtrl.SetStretch(m_bStretch);
}

void CGifAnimationTestDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	int iPos = m_SpeedSlider.GetPos();

	if (0 <= iPos && iPos <= _countof(SpeedInfo))
	{
		m_fSpeed = SpeedInfo[iPos].Speed;
		m_sDisplaySpeed = SpeedInfo[iPos].DisplayName;
	}

	m_iMaxFrameRate = m_MaxFrameRateSlider.GetPos();

	m_GifAnimationCtrl.SetSpeed(m_fSpeed);
	m_GifAnimationCtrl.SetMaxFrameRate(m_iMaxFrameRate);

	UpdateData(FALSE);

	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CGifAnimationTestDlg::OnEnChangeSpeed()
{
	if (GetFocus()->GetDlgCtrlID() == IDC_EDIT_SPEED)
	{
	}
}

void CGifAnimationTestDlg::OnEnChangeMaxFrameRate()
{
	if (GetFocus()->GetDlgCtrlID() == IDC_EDIT_MAX_FRAME_RATE)
	{
		UpdateData();

		if (m_iMaxFrameRate < (UINT) m_MaxFrameRateSlider.GetRangeMin())
		{
			m_iMaxFrameRate = (UINT) m_MaxFrameRateSlider.GetRangeMin();
		}

		if (m_iMaxFrameRate > (UINT) m_MaxFrameRateSlider.GetRangeMax())
		{
			m_iMaxFrameRate = (UINT) m_MaxFrameRateSlider.GetRangeMax();
		}

		m_MaxFrameRateSlider.SetPos(m_iMaxFrameRate);
		m_GifAnimationCtrl.SetMaxFrameRate(m_iMaxFrameRate);
	}
}

BEGIN_EVENTSINK_MAP(CGifAnimationTestDlg, CDialog)
	ON_EVENT(CGifAnimationTestDlg, IDC_GIF_ANIMATION_CTRL, 101, CGifAnimationTestDlg::OnFrameChangedGifAnimationCtrl, VTS_I4)
END_EVENTSINK_MAP()

void CGifAnimationTestDlg::OnFrameChangedGifAnimationCtrl(long iFrameIndex)
{
	m_sFrameInfo.Format(_T("Frame: %d  Delay: %d"), iFrameIndex, m_GifAnimationCtrl.GetFrameDelay(iFrameIndex));
	UpdateData(FALSE);
}
